package com.example.library_base.common

import android.content.Context
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.library_base.base.adapter.BaseViewHolder

@BindingAdapter("android:image", "android:circle", "android:placeholder", "android:error", requireAll = false)
fun loadImage(view: ImageView, url: String?, circle: Boolean , placeholder: Int, error: Int){
    if (url == null){
        view.setImageResource(placeholder)
    }else{
        if (circle){
            Glide.with(view.context).load(url).placeholder(placeholder).error(error).circleCrop().into(view)
        }else{
            Glide.with(view.context).load(url).placeholder(placeholder).error(error).into(view)
        }
    }
}

@BindingAdapter(value = ["bindAdapter"], requireAll = false)
fun <B: RecyclerView.Adapter<BaseViewHolder>> bindAdapter(view: RecyclerView, adapter: B){
    view.adapter = adapter
}

@BindingAdapter("android:context", requireAll = false)
fun bindToolbarBack(view: Toolbar, context: Context){
    view.setNavigationOnClickListener {
        (context as AppCompatActivity).onBackPressed()
    }
}